package com.fzdy.dataplatform.dao.impl;

import com.fzdy.dataplatform.core.dao.impl.BaseDaoImpl;
import com.fzdy.dataplatform.dao.KTVGroupDao;
import com.fzdy.dataplatform.entity.KTVGroup;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.*;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;

@Repository
public class KTVGroupDaoImpl extends BaseDaoImpl<KTVGroup> implements KTVGroupDao{

    @Override
    public List<KTVGroup> findByExampleLike(KTVGroup example, int pageNo, int pageSize) {
        Criteria criteria = this.getSession().createCriteria(this.clazz).add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        if (example.getUser() != null){
            criteria.createAlias("user","user");//添加外键关联。
            criteria.add(Restrictions.like("user.name",example.getUser().getName(), MatchMode.ANYWHERE));
        }
        return this.createPageOrder(criteria, pageNo, pageSize, Order.desc("id")).list();
    }

    @Override
    public long countByExampleLike(KTVGroup example) {
        Criteria criteria = this.getSession().createCriteria(this.clazz).add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        if (example.getUser() != null){
            criteria.createAlias("user","user");//添加外键关联。
            criteria.add(Restrictions.like("user.name",example.getUser().getName(), MatchMode.ANYWHERE));
        }
        Long o = (Long) criteria.setProjection(Projections.count("id")).uniqueResult();
        return o.longValue();
    }

    public void batchDelete(Serializable[] ids) {
        super.batchDelete(ids);
        Query q =getSession().createQuery("delete from UserKtv where ktvgroupid is null");
        Query q1 =getSession().createQuery("delete from UserKtvBusiness where ktvgroupid is null");
        q.executeUpdate();
        q1.executeUpdate();
    }

}
